﻿using Mes.Pendant.Common;
using Mes.Pendant.SupplierInterface;
using Mes.Pendant.SupplierInterface.SupplierDto;
using Mes.Pendant.SupplierManagements;
using Mes.Pendant.Warehousings;
using Mes.Pendant.Warehousings.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Mes.Pendant.WarehousingServices
{
    public class WarehousingService : ApplicationService, IWarehousingService
    {
        private readonly IRepository<Warehousing, int> _repository;

        public WarehousingService(IRepository<Warehousing, int> repository)
        {
            _repository = repository;
        }

        public async Task<int> AddWarehousing(WarehousingDto warehousingDto)
        {
            var warehousingCode = _repository.AnyAsync(m => m.WarehousingCode == warehousingDto.WarehousingCode).Result;
            if (warehousingCode)
            {
                return -1;
            }

            var warehousingName = _repository.AnyAsync(m => m.WarehousingName == warehousingDto.WarehousingName).Result;
            if (warehousingName)
            {
                return -1;
            }

            var warehousingModel = warehousingDto.MapTo<Warehousing>();
            await _repository.InsertAsync(warehousingModel);
            return warehousingDto.Id;
        }

        public async Task<PagedResult> GetWarehousingList(string? warehousingCode, string? warehousingName, bool? state, int page = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<Warehousing>(true);
            if (!string.IsNullOrEmpty(warehousingCode))
            {
                predicate = predicate.And(m => m.WarehousingCode.Contains(warehousingCode));
            }
            if (!string.IsNullOrEmpty(warehousingName))
            {
                predicate = predicate.And(m => m.WarehousingName.Contains(warehousingName));
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<int> DeleteWarehousing(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
        }

        public async Task<int> UpdateWarehousing(WarehousingDto WarehousingDto)
        {
            var warehousingName = await _repository.AnyAsync(m => m.WarehousingName == WarehousingDto.WarehousingName && m.Id != WarehousingDto.Id);

            Warehousing warehousing = WarehousingDto.MapTo<Warehousing>();
            await _repository.UpdateAsync(warehousing);
            return warehousing.Id;
        }

        public async Task<WarehousingDto> GetWarehousingInfo(int id)
        {
            var entity = _repository.GetAsync(id).Result;
            var warehousingDto = ObjectMapper.Map<Warehousing, WarehousingDto>(entity);
            return warehousingDto;
        }
    }
}
